package constant.milk.cashbook.database;

import java.util.ArrayList;

import constant.milk.cashbook.apt.AptSetting;
import constant.milk.cashbook.bean.AutoMainData;
import constant.milk.cashbook.bean.BankData;
import constant.milk.cashbook.bean.BudgetData;
import constant.milk.cashbook.bean.CategoryMainData;
import constant.milk.cashbook.bean.CategorySubData;
import constant.milk.cashbook.bean.CheckData;
import constant.milk.cashbook.bean.CreditData;
import constant.milk.cashbook.bean.GiftData;
import constant.milk.cashbook.bean.HalbuData;
import constant.milk.cashbook.bean.AptData;
import constant.milk.cashbook.bean.InOutData;
import constant.milk.cashbook.bean.MainSettingData;
import constant.milk.cashbook.bean.MyHouseData;
import constant.milk.cashbook.bean.PhoneData;
import constant.milk.cashbook.bean.RealData;
import constant.milk.cashbook.bean.SmsData;
import constant.milk.cashbook.bean.StatsData;
import constant.milk.cashbook.bean.WidgetSettingData;
import constant.milk.cashbook.util.MilkUtil;
import android.content.Context;
import android.database.Cursor;

public class DBSelectQuery
{
	private Context context;
	private DbAdapter db;
	
	public DBSelectQuery(Context context, DbAdapter db)
	{
		this.context = context;
		this.db = db;
	}
	
	public void setBeginTansaction() throws Exception
	{
		db.beginTansaction(context);
	}
	
	public void setSuccessful() throws Exception
	{
		db.setTransactionSuccessful();
	}
	
	public void setEndTansaction() throws Exception
	{
		db.endTransaction(context);
	}
	
	/**
	 * 현금 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<RealData> getRealList()
	{
		ArrayList<RealData> list = new ArrayList<RealData>();
		
		String query = "SELECT * FROM " + TableName.REAL_MONEY + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int moneyIdx = c.getColumnIndex("money");  // 현금잔액
		int nameIdx = c.getColumnIndex("name");  // 현금이름
		int memoIdx = c.getColumnIndex("memo"); // 현금설명
		int countIdx = c.getColumnIndex("count"); // 현금우선순위
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			RealData data = new RealData();
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 현금 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public RealData getRealId(String id)
	{
		RealData data = new RealData();
		
		String query = "SELECT * FROM " + TableName.REAL_MONEY + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int moneyIdx = c.getColumnIndex("money");  // 현금잔액
		int nameIdx = c.getColumnIndex("name");  // 현금이름
		int memoIdx = c.getColumnIndex("memo"); // 현금설명
		int countIdx = c.getColumnIndex("count"); // 현금우선순위
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 현금 데이터를 반환한다.
	 * @param name
	 * @return
	 */
	public RealData getRealName(String name)
	{
		RealData data = new RealData();
		
		String query = "SELECT * FROM " + TableName.REAL_MONEY + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int moneyIdx = c.getColumnIndex("money");  // 현금잔액
		int nameIdx = c.getColumnIndex("name");  // 현금이름
		int memoIdx = c.getColumnIndex("memo"); // 현금설명
		int countIdx = c.getColumnIndex("count"); // 현금우선순위
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 현금 테이블의 같은 이름의 테이블이 있나 검사한다.
	 * @param name
	 * @return
	 */
	public int getRealCount(String name)
	{
		String query = "SELECT * FROM " + TableName.REAL_MONEY + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int count = c.getCount();
		
		if(c != null) c.close();
		
		return count;
	}
	
	/**
	 * 통장 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<BankData> getBankList()
	{
		ArrayList<BankData> list = new ArrayList<BankData>();
		
		String query = "SELECT * FROM " + TableName.BANK_BOOK + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int moneyIdx = c.getColumnIndex("money");  // 통장잔액
		int nameIdx = c.getColumnIndex("name");  // 통장이름
		int memoIdx = c.getColumnIndex("memo"); // 통장설명
		int countIdx = c.getColumnIndex("count"); // 통장우선순위
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			BankData data = new BankData();
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 통장 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public BankData getBankId(String id)
	{
		BankData data = new BankData();
		
		String query = "SELECT * FROM " + TableName.BANK_BOOK + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int moneyIdx = c.getColumnIndex("money");  // 통장잔액
		int nameIdx = c.getColumnIndex("name");  // 통장이름
		int memoIdx = c.getColumnIndex("memo"); // 통장설명
		int countIdx = c.getColumnIndex("count"); // 통장우선순위
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 통장 데이터를 반환한다.
	 * @param name
	 * @return
	 */
	public BankData getBankName(String name)
	{
		BankData data = new BankData();
		
		String query = "SELECT * FROM " + TableName.BANK_BOOK + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int moneyIdx = c.getColumnIndex("money");  // 통장잔액
		int nameIdx = c.getColumnIndex("name");  // 통장이름
		int memoIdx = c.getColumnIndex("memo"); // 통장설명
		int countIdx = c.getColumnIndex("count"); // 통장우선순위
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 통장 테이블의 같은 이름의 테이블이 있나 검사한다.
	 * @param name
	 * @return
	 */
	public int getBankCount(String name)
	{
		String query = "SELECT * FROM " + TableName.BANK_BOOK + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int count = c.getCount();
		
		if(c != null) c.close();
		
		return count;
	}
	
	/**
	 * 체크카드 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<CheckData> getCheckList()
	{
		ArrayList<CheckData> list = new ArrayList<CheckData>();
		
		String query = "SELECT * FROM " + TableName.CHECK_CARD + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int nameIdx = c.getColumnIndex("name");  // 체크카드이름
		int memoIdx = c.getColumnIndex("memo"); // 체크카드설명
		int countIdx = c.getColumnIndex("count"); // 체크카드우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 연결통장아이디
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			CheckData data = new CheckData();
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 체크카드 리스트를 반환한다.
	 * @param linkid
	 * @return
	 */
	public ArrayList<CheckData> getCheckLinkidList(String linkid)
	{
		ArrayList<CheckData> list = new ArrayList<CheckData>();
		
		String query = "SELECT * FROM " + TableName.CHECK_CARD + 
				" WHERE linkid = " + linkid + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int nameIdx = c.getColumnIndex("name");  // 체크카드이름
		int memoIdx = c.getColumnIndex("memo"); // 체크카드설명
		int countIdx = c.getColumnIndex("count"); // 체크카드우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 연결통장아이디
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			CheckData data = new CheckData();
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 체크카드 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public CheckData getCheckId(String id)
	{
		CheckData data = new CheckData();
		
		String query = "SELECT * FROM " + TableName.CHECK_CARD + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int nameIdx = c.getColumnIndex("name");  // 체크카드이름
		int memoIdx = c.getColumnIndex("memo"); // 체크카드설명
		int countIdx = c.getColumnIndex("count"); // 체크카드우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 연결통장아이디
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 체크카드 데이터를 반환한다.
	 * @param name
	 * @return
	 */
	public CheckData getCheckName(String name)
	{
		CheckData data = new CheckData();
		
		String query = "SELECT * FROM " + TableName.CHECK_CARD + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int nameIdx = c.getColumnIndex("name");  // 체크카드이름
		int memoIdx = c.getColumnIndex("memo"); // 체크카드설명
		int countIdx = c.getColumnIndex("count"); // 체크카드우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 연결통장아이디
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 체크카드 테이블의 같은 이름의 테이블이 있나 검사한다.
	 * @param name
	 * @return
	 */
	public int getCheckCount(String name)
	{
		String query = "SELECT * FROM " + TableName.CHECK_CARD + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int count = c.getCount();
		
		if(c != null) c.close();
		
		return count;
	}
	
	/**
	 * 신용카드 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<CreditData> getCreditList()
	{
		ArrayList<CreditData> list = new ArrayList<CreditData>();
		
		String query = "SELECT * FROM " + TableName.CREDIT_CARD + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int nameIdx = c.getColumnIndex("name");  // 신용카드이름
		int memoIdx = c.getColumnIndex("memo"); // 신용카드설명
		int countIdx = c.getColumnIndex("count"); // 신용카드우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 연결통장아이디
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			CreditData data = new CreditData();
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(MilkUtil.getCommaDel(c.getString(moneyIdx)));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 신용카드 리스트를 반환한다.
	 * @param linkid
	 * @return
	 */
	public ArrayList<CreditData> getCreditLinkIdList(String linkid)
	{
		ArrayList<CreditData> list = new ArrayList<CreditData>();
		
		String query = "SELECT * FROM " + TableName.CREDIT_CARD + 
				" WHERE linkid = " + linkid + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int nameIdx = c.getColumnIndex("name");  // 신용카드이름
		int memoIdx = c.getColumnIndex("memo"); // 신용카드설명
		int countIdx = c.getColumnIndex("count"); // 신용카드우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 연결통장아이디
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			CreditData data = new CreditData();
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(MilkUtil.getCommaDel(c.getString(moneyIdx)));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 신용카드 자동화 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<CreditData> getCreditListAuto()
	{
		ArrayList<CreditData> list = new ArrayList<CreditData>();
		
		String query = "SELECT * FROM " + TableName.CREDIT_CARD + " WHERE autoyn = 0 ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int nameIdx = c.getColumnIndex("name");  // 신용카드이름
		int memoIdx = c.getColumnIndex("memo"); // 신용카드설명
		int countIdx = c.getColumnIndex("count"); // 신용카드우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 연결통장아이디
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			CreditData data = new CreditData();
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(MilkUtil.getCommaDel(c.getString(moneyIdx)));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 신용카드 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public CreditData getCreditId(String id)
	{
		CreditData data = new CreditData();
		
		String query = "SELECT * FROM " + TableName.CREDIT_CARD + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int nameIdx = c.getColumnIndex("name");  // 이름
		int memoIdx = c.getColumnIndex("memo"); // 설명
		int countIdx = c.getColumnIndex("count"); // 우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 통장연결아이디
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(MilkUtil.getCommaDel(c.getString(moneyIdx)));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 신용카드 데이터를 반환한다.
	 * @param name
	 * @return
	 */
	public CreditData getCreditName(String name)
	{
		CreditData data = new CreditData();
		
		String query = "SELECT * FROM " + TableName.CREDIT_CARD + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int nameIdx = c.getColumnIndex("name");  // 이름
		int memoIdx = c.getColumnIndex("memo"); // 설명
		int countIdx = c.getColumnIndex("count"); // 우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 통장연결아이디
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(MilkUtil.getCommaDel(c.getString(moneyIdx)));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 신용카드 테이블의 같은 이름의 테이블이 있나 검사한다.
	 * @param name
	 * @return
	 */
	public int getCreditCount(String name)
	{
		String query = "SELECT * FROM " + TableName.CREDIT_CARD + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int count = c.getCount();
		
		if(c != null) c.close();
		
		return count;
	}
	
	/**
	 * 핸드폰 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<PhoneData> getPhoneList()
	{
		ArrayList<PhoneData> list = new ArrayList<PhoneData>();
		
		String query = "SELECT * FROM " + TableName.PHONE + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산타입
		int nameIdx = c.getColumnIndex("name");  // 핸드폰이름
		int memoIdx = c.getColumnIndex("memo"); // 핸드폰설명
		int countIdx = c.getColumnIndex("count"); // 핸드폰우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 통장연결아이디
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			PhoneData data = new PhoneData();
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 핸드폰 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<PhoneData> getPhoneLinkidList(String linkid)
	{
		ArrayList<PhoneData> list = new ArrayList<PhoneData>();
		
		String query = "SELECT * FROM " + TableName.PHONE + 
				" WHERE linkid = " + linkid + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산타입
		int nameIdx = c.getColumnIndex("name");  // 핸드폰이름
		int memoIdx = c.getColumnIndex("memo"); // 핸드폰설명
		int countIdx = c.getColumnIndex("count"); // 핸드폰우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 통장연결아이디
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			PhoneData data = new PhoneData();
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 핸드폰 자동화 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<PhoneData> getPhoneListAuto()
	{
		ArrayList<PhoneData> list = new ArrayList<PhoneData>();
		
		String query = "SELECT * FROM " + TableName.PHONE + " WHERE autoyn = 0 ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산타입
		int nameIdx = c.getColumnIndex("name");  // 핸드폰이름
		int memoIdx = c.getColumnIndex("memo"); // 핸드폰설명
		int countIdx = c.getColumnIndex("count"); // 핸드폰우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 핸드폰우선순위
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			PhoneData data = new PhoneData();
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 핸드폰 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public PhoneData getPhoneId(String id)
	{
		PhoneData data = new PhoneData();
		
		String query = "SELECT * FROM " + TableName.PHONE + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산타입
		int nameIdx = c.getColumnIndex("name");  // 이름
		int memoIdx = c.getColumnIndex("memo"); // 설명
		int countIdx = c.getColumnIndex("count"); // 우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 통장연결아이디
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 핸드폰 데이터를 반환한다.
	 * @param Name
	 * @return
	 */
	public PhoneData getPhoneName(String Name)
	{
		PhoneData data = new PhoneData();
		
		String query = "SELECT * FROM " + TableName.PHONE + " WHERE name = '" + 
						MilkUtil.getApostrophe(Name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int moneyIdx = c.getColumnIndex("money");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산타입
		int nameIdx = c.getColumnIndex("name");  // 이름
		int memoIdx = c.getColumnIndex("memo"); // 설명
		int countIdx = c.getColumnIndex("count"); // 우선순위
		int linkidIdx = c.getColumnIndex("linkid"); // 통장연결아이디
		int autoynIdx = c.getColumnIndex("autoyn"); // 카드대금결제 자동 유무
		int termdateIdx = c.getColumnIndex("termdate"); // 사용기간
		int resultdateIdx = c.getColumnIndex("resultdate"); // 결제일
		int signdateIdx = c.getColumnIndex("signdate"); // 마지막 결제 날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			data.setAutoyn(c.getInt(autoynIdx) + "");
			data.setTermdate(c.getInt(termdateIdx) + "");
			data.setResultdate(c.getInt(resultdateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 핸드폰 테이블의 같은 이름의 테이블이 있나 검사한다.
	 * @param name
	 * @return
	 */
	public int getPhoneCount(String name)
	{
		String query = "SELECT * FROM " + TableName.PHONE + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int count = c.getCount();
		
		if(c != null) c.close();
		
		return count;
	}
	
	/**
	 * 상품권 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<GiftData> getGiftList()
	{
		ArrayList<GiftData> list = new ArrayList<GiftData>();
		
		String query = "SELECT * FROM " + TableName.GIFT_CARD + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int moneyIdx = c.getColumnIndex("money");  // 상품권금액
		int nameIdx = c.getColumnIndex("name");  // 상품권이름
		int memoIdx = c.getColumnIndex("memo"); // 상품권설명
		int countIdx = c.getColumnIndex("count"); // 상품권우선순위
		int giftnumberIdx = c.getColumnIndex("giftnumber"); // 상품권 갯수
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			GiftData data = new GiftData();
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setNumber(c.getInt(giftnumberIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 상품권 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public GiftData getGiftId(String id)
	{
		GiftData data = new GiftData();
		
		String query = "SELECT * FROM " + TableName.GIFT_CARD + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int moneyIdx = c.getColumnIndex("money");  // 상품권금액
		int nameIdx = c.getColumnIndex("name");  // 상품권이름
		int memoIdx = c.getColumnIndex("memo"); // 상품권설명
		int countIdx = c.getColumnIndex("count"); // 상품권우선순위
		int giftnumberIdx = c.getColumnIndex("giftnumber"); // 상품권 갯수
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setNumber(c.getInt(giftnumberIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 상품권 데이터를 반환한다.
	 * @param name
	 * @return
	 */
	public GiftData getGiftName(String name)
	{
		GiftData data = new GiftData();
		
		String query = "SELECT * FROM " + TableName.GIFT_CARD + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int addflagIdx = c.getColumnIndex("addflag");  // 자산포함유무
		int moneyIdx = c.getColumnIndex("money");  // 상품권금액
		int nameIdx = c.getColumnIndex("name");  // 상품권이름
		int memoIdx = c.getColumnIndex("memo"); // 상품권설명
		int countIdx = c.getColumnIndex("count"); // 상품권우선순위
		int giftnumberIdx = c.getColumnIndex("giftnumber"); // 상품권 갯수
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setAddflag(c.getInt(addflagIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			data.setName(c.getString(nameIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setNumber(c.getInt(giftnumberIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 상품권 테이블의 같은 이름의 테이블이 있나 검사한다.
	 * @param name
	 * @return
	 */
	public int getGiftCardCount(String name)
	{
		String query = "SELECT * FROM " + TableName.GIFT_CARD + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int count = c.getCount();
		
		if(c != null) c.close();
		
		return count;
	}
	
	/**
	 * 메인카테고리 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<CategoryMainData> getCategoryMainList()
	{
		ArrayList<CategoryMainData> list = new ArrayList<CategoryMainData>();
		
		String query = "SELECT * FROM " + TableName.CATEGORY_MAIN + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");  // 이름
		int countIdx = c.getColumnIndex("count");  // 빈도수
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			CategoryMainData data = new CategoryMainData();
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setCount(c.getInt(countIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 메인카테고리 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public CategoryMainData getCategoryMainId(String id)
	{
		CategoryMainData data = new CategoryMainData();
		
		String query = "SELECT * FROM " + TableName.CATEGORY_MAIN + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");  // 이름
		int countIdx = c.getColumnIndex("count");  // 빈도수
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setCount(c.getInt(countIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 메인카테고리 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public CategoryMainData getCategoryMainName(String name)
	{
		CategoryMainData data = new CategoryMainData();
		
		String query = "SELECT * FROM " + TableName.CATEGORY_MAIN + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");  // 이름
		int countIdx = c.getColumnIndex("count");  // 빈도수
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setCount(c.getInt(countIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 메인카테고리 테이블의 같은 이름의 테이블이 있나 검사한다.
	 * @param name
	 * @return
	 */
	public int getCategoryMainCount(String name)
	{
		String query = "SELECT * FROM " + TableName.CATEGORY_MAIN + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int count = c.getCount();
		
		if(c != null) c.close();
		
		return count;
	}
	
	/**
	 * 서브카테고리 리스트를 반환한다.
	 * @return
	 */
	public ArrayList<CategorySubData> getCategorySubList(String id)
	{
		ArrayList<CategorySubData> list = new ArrayList<CategorySubData>();
		
		String query = "SELECT * FROM " + TableName.CATEGORY_SUB + 
				" WHERE linkid = " + id + " ORDER BY count ASC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");  // 이름
		int countIdx = c.getColumnIndex("count");  // 빈도수
		int linkidIdx = c.getColumnIndex("linkid");  // 상위카테고리아이디
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			CategorySubData data = new CategorySubData();
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 서브카테고리 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public CategorySubData getCategorySubId(String id)
	{
		CategorySubData data = new CategorySubData();
		
		String query = "SELECT * FROM " + TableName.CATEGORY_SUB + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");  // 이름
		int countIdx = c.getColumnIndex("count");  // 빈도수
		int linkidIdx = c.getColumnIndex("linkid");  // 상위카테고리아이디
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 서브카테고리 데이터를 반환한다.
	 * @param id
	 * @return
	 */
	public CategorySubData getCategorySubName(String name)
	{
		CategorySubData data = new CategorySubData();
		
		String query = "SELECT * FROM " + TableName.CATEGORY_SUB + " WHERE name = '" + 
						MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");  // 이름
		int countIdx = c.getColumnIndex("count");  // 빈도수
		int linkidIdx = c.getColumnIndex("linkid");  // 상위카테고리아이디
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setCount(c.getInt(countIdx) + "");
			data.setLinkid(c.getInt(linkidIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * 서브카테고리 테이블의 같은 이름의 테이블이 있나 검사한다.
	 * @param name
	 * @return
	 */
	public int getCategorySubCount(String id, String name)
	{
		String query = "SELECT * FROM " + TableName.CATEGORY_SUB + 
				" WHERE linkid = " + id + " and name = '" + MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int count = c.getCount();
		
		if(c != null) c.close();
		
		return count;
	}
	
	/**
	 * 0 = 지출, 1 = 수입, 2 = 이동, 3 = 부채, 4 = 신용카드, 5 = 소액결제, 6 = 상품권사용, 7 = 상품권구입, 8 = 대금처리
	 * @param typeInt
	 * @return
	 */
	public ArrayList<InOutData> getInOutList(int typeInt, String assetStr, String nameStr, 
			String mainCategory, String subCategory, String memoStr, String firstDate, String lastDate)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		String query = "";
		
		if (typeInt == 0)
		{
			query = "SELECT * FROM " + TableName.IN_OUT + " WHERE type in (0, 4, 5, 6, 8)";
		}
		else if (typeInt == -1)
		{
			query = "SELECT * FROM " + TableName.IN_OUT + " WHERE type in (0, 1, 2, 3, 4, 5, 6, 7, 8)";
		}
		else if (typeInt == 2)
		{
			query = "SELECT * FROM " + TableName.IN_OUT + " WHERE type in (2, 7)";
		}
		else
		{
			query = "SELECT * FROM " + TableName.IN_OUT + " WHERE type = "+ typeInt; 
		}
		
		if (!assetStr.equals("6"))
		{
			if (nameStr.equals(""))
			{
				query += " and asset = " + assetStr;
			}
			else
			{
				query += " and asset = " + assetStr + " and name = '" + MilkUtil.getApostrophe(nameStr) + "'";
			}
		}
		
		if (!mainCategory.equals(""))
		{
			query += " and maincategory = '" + mainCategory + "'";
			
			if (!subCategory.equals(""))
			{
				query += " and subcategory = '" + subCategory + "'";
			}
		}
		
		if (!memoStr.equals(""))
		{
			query += " and memo like '%" + memoStr + "%'";
		}
		
		query += " and date >= " + firstDate + " and date <= " + lastDate + 
				 " ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int date = c.getColumnIndex("date"); // 날짜
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int point = c.getColumnIndex("point"); // 카드포인트
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int person = c.getColumnIndex("person"); // 부채사람
		int photopath = c.getColumnIndex("photopath"); // 사진연결주소
		int memo = c.getColumnIndex("memo"); // 내용
		int linkid = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(id) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setDate(c.getInt(date) + "");
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setPoint(c.getString(point));
			data.setSusumoney(c.getString(susumoney));
			data.setPerson(c.getString(person));
			data.setPhotopath(c.getString(photopath));
			data.setMemo(c.getString(memo));
			data.setLinkId(c.getInt(linkid) + "");
			data.setSubmoney(c.getString(submoneyid));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 0 = 지출, 1 = 수입, 2 = 이동, 3 = 부채
	 * @param flag
	 * @return
	 */
	public ArrayList<InOutData> getInOutListFan(int flag, String assetStr, String nameStr, 
			String mainCategory, String subCategory, String memoStr, String firstDate, String lastDate)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		String query = "";
		
		if (assetStr.equals("6"))
		{
			query = "SELECT * FROM " + TableName.IN_OUT + " WHERE type = 3" + " and kind = " + flag;
		}
		else
		{
			if (nameStr.equals(""))
			{
				query = "SELECT * FROM " + TableName.IN_OUT + " WHERE type = 3" + " and kind = " + flag +
						" and asset = " + assetStr;
			}
			else
			{
				query = "SELECT * FROM " + TableName.IN_OUT + " WHERE type = 3" + " and kind = " + flag +
						" and asset = " + assetStr + " and name = '" + MilkUtil.getApostrophe(nameStr) + "'";
			}
		}
		
		if (!mainCategory.equals(""))
		{
			query += " and maincategory = '" + mainCategory + "'";
			
			if (!subCategory.equals(""))
			{
				query += " and subcategory = '" + subCategory + "'";
			}
		}
		
		if (!memoStr.equals(""))
		{
			query += " and memo like '#" + memoStr + "#'";
		}
		
		query += " and date >= " + firstDate + " and date <= " + lastDate + 
				" ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int date = c.getColumnIndex("date"); // 날짜
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int point = c.getColumnIndex("point"); // 카드포인트
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int person = c.getColumnIndex("person"); // 부채사람
		int photopath = c.getColumnIndex("photopath"); // 사진연결주소
		int memo = c.getColumnIndex("memo"); // 내용
		int linkid = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(id) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setDate(c.getInt(date) + "");
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setSubmoney(c.getString(submoneyid));
			data.setPoint(c.getString(point));
			data.setSusumoney(c.getString(susumoney));
			data.setPerson(c.getString(person));
			data.setPhotopath(c.getString(photopath));
			data.setMemo(c.getString(memo));
			data.setLinkId(c.getInt(linkid) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 0 = 지출, 1 = 수입, 2 = 이동, 3 = 부채
	 * @param flag
	 * @return
	 */
	public ArrayList<InOutData> getInOutListFanLinkId(int flag, String selectId)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.IN_OUT + " WHERE type = 3" + " and kind = " + flag +
				" and linkid = " + selectId +
				" ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int date = c.getColumnIndex("date"); // 날짜
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int point = c.getColumnIndex("point"); // 카드포인트
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int person = c.getColumnIndex("person"); // 부채사람
		int photopath = c.getColumnIndex("photopath"); // 사진연결주소
		int memo = c.getColumnIndex("memo"); // 내용
		int linkid = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(id) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setDate(c.getInt(date) + "");
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setSubmoney(c.getString(submoneyid));
			data.setPoint(c.getString(point));
			data.setSusumoney(c.getString(susumoney));
			data.setPerson(c.getString(person));
			data.setPhotopath(c.getString(photopath));
			data.setMemo(c.getString(memo));
			data.setLinkId(c.getInt(linkid) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * @return
	 */
	public InOutData getInOutId(String inOutId)
	{
		InOutData data = new InOutData();
		
		String query = "SELECT * FROM " + TableName.IN_OUT + " WHERE id = "+ inOutId + ";";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int date = c.getColumnIndex("date"); // 날짜
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int point = c.getColumnIndex("point"); // 카드포인트
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int person = c.getColumnIndex("person"); // 부채사람
		int photopath = c.getColumnIndex("photopath"); // 사진연결주소
		int memo = c.getColumnIndex("memo"); // 내용
		int linkid = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(id) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setDate(c.getInt(date) + "");
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setPoint(c.getString(point));
			data.setSusumoney(c.getString(susumoney));
			data.setPerson(c.getString(person));
			data.setPhotopath(c.getString(photopath));
			data.setMemo(c.getString(memo));
			data.setLinkId(c.getInt(linkid) + "");
			data.setSubmoney(c.getString(submoneyid));
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * @return
	 */
	public ArrayList<InOutData> getInOutLinkId(String inOutId)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		String query = "SELECT * FROM " + TableName.IN_OUT + " WHERE linkid = "+ inOutId + ";";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int date = c.getColumnIndex("date"); // 날짜
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int point = c.getColumnIndex("point"); // 카드포인트
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int person = c.getColumnIndex("person"); // 부채사람
		int photopath = c.getColumnIndex("photopath"); // 사진연결주소
		int memo = c.getColumnIndex("memo"); // 내용
		int linkid = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(id) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setDate(c.getInt(date) + "");
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setPoint(c.getString(point));
			data.setSusumoney(c.getString(susumoney));
			data.setPerson(c.getString(person));
			data.setPhotopath(c.getString(photopath));
			data.setMemo(c.getString(memo));
			data.setLinkId(c.getInt(linkid) + "");
			data.setSubmoney(c.getString(submoneyid));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 0 = 지출, 1 = 수입, 2 = 이동
	 * @param flag
	 * @return
	 */
	public ArrayList<AutoMainData> getAutoMonthList(int flag)
	{
		ArrayList<AutoMainData> list = new ArrayList<AutoMainData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.AUTO_MAIN + " WHERE type = " + flag +
				" and monthweekday = 0 ORDER BY autodate DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int monthweekday = c.getColumnIndex("monthweekday"); // 주간 월간 플래그
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int memo = c.getColumnIndex("memo"); // 내용
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		int autodateid = c.getColumnIndex("autodate"); // 고정날짜
		int signdateid = c.getColumnIndex("signdate"); // 적용날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			AutoMainData data = new AutoMainData();
			
			data.setId(c.getInt(id) + "");
			data.setMonthweekday(c.getInt(monthweekday) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setSusumoney(c.getString(susumoney));
			data.setMemo(c.getString(memo));
			data.setSubmoney(c.getString(submoneyid));
			data.setAutodate(c.getInt(autodateid) + "");
			data.setSigndate(c.getInt(signdateid) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * 0 = 지출, 1 = 수입, 2 = 이동
	 * @param flag
	 * @return
	 */
	public ArrayList<AutoMainData> getAutoWeekdayList(int flag)
	{
		ArrayList<AutoMainData> list = new ArrayList<AutoMainData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.AUTO_MAIN + " WHERE type = "+ flag +
				" and monthweekday = 1 ORDER BY autodate DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int monthweekday = c.getColumnIndex("monthweekday"); // 주간 월간 플래그
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int memo = c.getColumnIndex("memo"); // 내용
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		int autodateid = c.getColumnIndex("autodate"); // 고정날짜
		int signdateid = c.getColumnIndex("signdate"); // 적용날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			AutoMainData data = new AutoMainData();
			
			data.setId(c.getInt(id) + "");
			data.setMonthweekday(c.getInt(monthweekday) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setSusumoney(c.getString(susumoney));
			data.setMemo(c.getString(memo));
			data.setSubmoney(c.getString(submoneyid));
			data.setAutodate(c.getInt(autodateid) + "");
			data.setSigndate(c.getInt(signdateid) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	public AutoMainData getAutoId(String autoId)
	{
		AutoMainData data = new AutoMainData();
		
		String query = "SELECT * FROM " + TableName.AUTO_MAIN + " WHERE id = " + autoId + "";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int monthweekday = c.getColumnIndex("monthweekday"); // 주간 월간 플래그
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int memo = c.getColumnIndex("memo"); // 내용
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		int autodateid = c.getColumnIndex("autodate"); // 고정날짜
		int signdateid = c.getColumnIndex("signdate"); // 적용날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(id) + "");
			data.setMonthweekday(c.getInt(monthweekday) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setSusumoney(c.getString(susumoney));
			data.setMemo(c.getString(memo));
			data.setSubmoney(c.getString(submoneyid));
			data.setAutodate(c.getInt(autodateid) + "");
			data.setSigndate(c.getInt(signdateid) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	public ArrayList<SmsData> getSmsList()
	{
		ArrayList<SmsData> list = new ArrayList<SmsData>();
		
		String query = "SELECT * FROM " + TableName.SMS + ";";
		
		Cursor c= db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");
		int parserIdx = c.getColumnIndex("parser");
		int addressIdx = c.getColumnIndex("address");
		int memoIdx = c.getColumnIndex("memo");
		int cardnameIdx = c.getColumnIndex("cardname");
		int assetIdx = c.getColumnIndex("asset");
		int subcardnameIdx = c.getColumnIndex("subcardname");
		int subassetIdx = c.getColumnIndex("subasset");
		int typeIdx = c.getColumnIndex("type");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			SmsData data = new SmsData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setParser(c.getInt(parserIdx) + "");
			data.setAddress(c.getString(addressIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCardname(c.getString(cardnameIdx));
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubCardname(c.getString(subcardnameIdx));
			data.setSubAsset(c.getInt(subassetIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	public SmsData getSmsId(String id)
	{
		SmsData data = new SmsData();
		
		String query = "SELECT * FROM " + TableName.SMS + " WHERE id = " + id + ";";
		
		Cursor c= db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");
		int parserIdx = c.getColumnIndex("parser");
		int addressIdx = c.getColumnIndex("address");
		int memoIdx = c.getColumnIndex("memo");
		int cardnameIdx = c.getColumnIndex("cardname");
		int assetIdx = c.getColumnIndex("asset");
		int subcardnameIdx = c.getColumnIndex("subcardname");
		int subassetIdx = c.getColumnIndex("subasset");
		int typeIdx = c.getColumnIndex("type");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setParser(c.getInt(parserIdx) + "");
			data.setAddress(c.getString(addressIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCardname(c.getString(cardnameIdx));
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubCardname(c.getString(subcardnameIdx));
			data.setSubAsset(c.getInt(subassetIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	public SmsData getSmsSelect(String address, String assetName)
	{
		SmsData data = new SmsData();
		
		String query = "SELECT * FROM " + TableName.SMS + " WHERE address = '" + 
						MilkUtil.getApostrophe(address) + "' and name = '" + MilkUtil.getApostrophe(assetName) + "';";
		
		Cursor c= db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");
		int parserIdx = c.getColumnIndex("parser");
		int addressIdx = c.getColumnIndex("address");
		int memoIdx = c.getColumnIndex("memo");
		int cardnameIdx = c.getColumnIndex("cardname");
		int assetIdx = c.getColumnIndex("asset");
		int subcardnameIdx = c.getColumnIndex("subcardname");
		int subassetIdx = c.getColumnIndex("subasset");
		int typeIdx = c.getColumnIndex("type");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setParser(c.getInt(parserIdx) + "");
			data.setAddress(c.getString(addressIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCardname(c.getString(cardnameIdx));
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubCardname(c.getString(subcardnameIdx));
			data.setSubAsset(c.getInt(subassetIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * @return
	 */
	public ArrayList<HalbuData> getHalbuNoList()
	{
		ArrayList<HalbuData> list = new ArrayList<HalbuData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.HALBU + 
				" WHERE halbucount > resultcount " +
				" ORDER BY autodate DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int halbucount = c.getColumnIndex("halbucount"); // 개월수
		int resultcount = c.getColumnIndex("resultcount"); // 남은 개월수
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int memo = c.getColumnIndex("memo"); // 내용
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		int autodateid = c.getColumnIndex("autodate"); // 고정날짜
		int signdateid = c.getColumnIndex("signdate"); // 적용날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			HalbuData data = new HalbuData();
			
			data.setId(c.getInt(id) + "");
			data.setHalbucount(c.getInt(halbucount) + "");
			data.setResultcount(c.getInt(resultcount) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setSusumoney(c.getString(susumoney));
			data.setMemo(c.getString(memo));
			data.setSubmoney(c.getString(submoneyid));
			data.setAutodate(c.getInt(autodateid) + "");
			data.setSigndate(c.getInt(signdateid) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * @return
	 */
	public ArrayList<HalbuData> getHalbuOkList()
	{
		ArrayList<HalbuData> list = new ArrayList<HalbuData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.HALBU + 
				" WHERE halbucount <= resultcount" +
				" ORDER BY autodate DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int halbucount = c.getColumnIndex("halbucount"); // 개월수
		int resultcount = c.getColumnIndex("resultcount"); // 남은 개월수
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int memo = c.getColumnIndex("memo"); // 내용
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		int autodateid = c.getColumnIndex("autodate"); // 고정날짜
		int signdateid = c.getColumnIndex("signdate"); // 적용날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			HalbuData data = new HalbuData();
			
			data.setId(c.getInt(id) + "");
			data.setHalbucount(c.getInt(halbucount) + "");
			data.setResultcount(c.getInt(resultcount) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setSusumoney(c.getString(susumoney));
			data.setMemo(c.getString(memo));
			data.setSubmoney(c.getString(submoneyid));
			data.setAutodate(c.getInt(autodateid) + "");
			data.setSigndate(c.getInt(signdateid) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	public HalbuData getHalbuId(String halbuId)
	{
		HalbuData data = new HalbuData();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.HALBU + 
				" WHERE id = " + halbuId + ";";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int halbucount = c.getColumnIndex("halbucount"); // 개월수
		int resultcount = c.getColumnIndex("resultcount"); // 남은 개월수
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int memo = c.getColumnIndex("memo"); // 내용
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		int autodateid = c.getColumnIndex("autodate"); // 고정날짜
		int signdateid = c.getColumnIndex("signdate"); // 적용날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(id) + "");
			data.setHalbucount(c.getInt(halbucount) + "");
			data.setResultcount(c.getInt(resultcount) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setSusumoney(c.getString(susumoney));
			data.setMemo(c.getString(memo));
			data.setSubmoney(c.getString(submoneyid));
			data.setAutodate(c.getInt(autodateid) + "");
			data.setSigndate(c.getInt(signdateid) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * flag  0 = 지출, 1 = 수입, 2 = 이동, 3 = 거스름돈
	 * type 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 카테고리
	 * @param flag
	 * @param type
	 * @param firstDate
	 * @param lastDate
	 * @return
	 */
	public ArrayList<StatsData> getInOutStatsList(int flag, int asset, String firstDate, String lastDate)
	{
		ArrayList<StatsData> list = new ArrayList<StatsData>();
		
		String query = "";
		
		// type 0 = 지출, 1 = 수입, 2 = 이동, 3 = 부채, 4 = 신용카드, 5 = 소액결제, 6 = 상품권사용, 7 = 상품권구입, 8 = 대금처리
		if (flag == 0)
		{
			if (asset == 0)
			{
				query = "SELECT SUM(totalmoney) totalmoney, title FROM (" +
						"SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 0 and type in (0)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" UNION ALL " +
						"SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 0 and type in (6) and kind in (6)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						") GROUP BY title";
			}
			else if (asset == 1)
			{
				query = "SELECT SUM(totalmoney) totalmoney, title FROM (" +
						"SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 1 and type in (0)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" UNION ALL " +
						"SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 1 and type in (6) and kind in (6)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						") GROUP BY title";
			}
			else if (asset == 2)
			{
				query = "SELECT SUM(totalmoney) totalmoney, title FROM (" +
						"SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 2 and type in (0)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" UNION ALL " +
						"SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 2 and type in (6) and kind in (6)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						") GROUP BY title";
			}
			else if (asset == 3)
			{
				query = "SELECT SUM(totalmoney) totalmoney, title FROM (" +
						"SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 3 and type in (4)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" UNION ALL " +
						"SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 3 and type in (6) and kind in (6)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						") GROUP BY title";
			}
			else if (asset == 4)
			{
				query = "SELECT SUM(totalmoney) totalmoney, title FROM (" +
						"SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 4 and type in (5)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" UNION ALL " +
						"SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 4 and type in (6) and kind in (6)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						") GROUP BY title";
			}
			else if (asset == 5)
			{
				query = "SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 5 and type in (6)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
			}
		}
		else if (flag == 1)
		{
			if (asset == 0)
			{
				query = "SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 0 and type in (1)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
			}
			else if (asset == 1)
			{
				query = "SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 1 and type in (1)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
			}
			else if (asset == 2)
			{
				query = "SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 2 and type in (1)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
			}
		}
		else if (flag == 2)
		{
			if (asset == 0)
			{
				query = "SELECT SUM(totalmoney) totalmoney, title FROM (" +
						"SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 0 and type in (2)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" UNION ALL " +
						"SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 0 and type in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						") GROUP BY title";
			}
			else if (asset == 1)
			{
				query = "SELECT SUM(totalmoney) totalmoney, title FROM (" +
						"SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 1 and type in (2)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" UNION ALL " +
						"SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 1 and type in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						") GROUP BY title";
			}
			else if (asset == 2)
			{
				query = "SELECT SUM(totalmoney) totalmoney, title FROM (" +
						"SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 2 and type in (2)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" UNION ALL " +
						"SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 2 and type in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						") GROUP BY title";
			}
			else if (asset == 3)
			{
				query = "SELECT SUM(totalmoney) totalmoney, title FROM (" +
						"SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 3 and type in (2)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" UNION ALL " +
						"SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 3 and type in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						") GROUP BY title";
			}
			else if (asset == 5)
			{
				query = "SELECT SUM(money) totalmoney, name title FROM " + TableName.IN_OUT + 
						" WHERE asset = 5 and type in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title" +
						" ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
			}
		}
		else if (flag == -1)
		{
			if (asset == 0)
			{
				query = "SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 0 and type in (6) and kind in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title";
			}
			else if (asset == 1)
			{
				query = "SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 1 and type in (6) and kind in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title";
			}
			else if (asset == 2)
			{
				query = "SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 2 and type in (6) and kind in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title";
			}
			else if (asset == 3)
			{
				query = "SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 3 and type in (6) and kind in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title";
			}
			else if (asset == 4)
			{
				query = "SELECT SUM(submoney) totalmoney, subname title FROM " + TableName.IN_OUT + 
						" WHERE subasset = 4 and type in (6) and kind in (7)" + 
						" and date >= " + firstDate + " and date <= " + lastDate + 
						" GROUP BY title";
			}
		}
		
		if (query.equals(""))
		{
			return list;
		}
		
		Cursor c = db.select(context, query);
		
		int titleIdx = c.getColumnIndex("title");
		int moneyIdx = c.getColumnIndex("totalmoney");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			StatsData data = new StatsData();
			
			data.setTitle(c.getString(titleIdx));
			data.setMoney(c.getString(moneyIdx));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * type : 0 = 지출, 1 = 수입, 2 = 이동 
	 * kind : 0 = 주간, 1 = 월간, 2 = 연간  
	 * @param type
	 * @param kind
	 * @return
	 */
	public BudgetData getBudget(int type, int kind)
	{
		BudgetData data = new BudgetData();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.BUDGET + 
				" WHERE type = " + type + " and kind = " + kind + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type"); // 타입
		int kindIdx = c.getColumnIndex("kind"); // 종류
		int moneyIdx = c.getColumnIndex("money"); // 예산금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setKind(c.getInt(kindIdx) + "");
			data.setMoney(c.getString(moneyIdx));
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * type 0 = 지출, 1 = 수입, 2 = 이동, 4 = 잔액
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체 
	 * @return
	 */
	public ArrayList<MainSettingData> getMainSettingList()
	{
		ArrayList<MainSettingData> list = new ArrayList<MainSettingData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.MAIN_SETTING + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type"); // 타입
		int assetIdx = c.getColumnIndex("asset"); // 자산
		int nameIdx = c.getColumnIndex("name"); // 자산이름
		int kindIdx = c.getColumnIndex("kind"); // 종류
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			MainSettingData data = new MainSettingData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setKind(c.getInt(kindIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * type 0 = 지출, 1 = 수입, 2 = 이동, 3 = 부채
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체 
	 * @param type
	 * @param asset
	 * @param name
	 * @param firstDate
	 * @param lastDate
	 * @return
	 */
	public ArrayList<InOutData> getInOutMainList(int type, int asset, String name, String firstDate, String lastDate)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		String query = "SELECT * FROM " + TableName.IN_OUT;
		
		// type 0 = 지출, 1 = 수입, 2 = 이동, 3 = 부채, 4 = 신용카드, 5 = 소액결제, 6 = 상품권사용, 7 = 상품권구입, 8 = 대금처리
		if (type == 0)
		{
			if (asset == 6) // 전체
			{
				query += " WHERE type in (0, 4, 5, 6, 8)";
			}
			else if (asset == 0) // 현금
			{
				query += " WHERE type in (0, 6) and (asset = " + asset + " or subasset = " + asset + ")";
			}
			else if (asset == 1) // 통장
			{
				query += " WHERE type in (0, 6) and (asset = " + asset + " or subasset = " + asset + ")";
			}
			else if (asset == 2) // 체크
			{
				query += " WHERE type in (0, 6) and (asset = " + asset + " or subasset = " + asset + ")";
			}
			else if (asset == 3) // 신용
			{
				query += " WHERE type in (2, 4, 6) and (asset = " + asset + " or subasset = " + asset + ")";
			}
			else if (asset == 4) // 핸드폰
			{
				query += " WHERE type in (5, 6) and (asset = " + asset + " or subasset = " + asset + ")";
			}
			else if (asset == 5) // 상품권
			{
				query += " WHERE type = 6 and asset = " + asset;
			}
		}
		else if (type == 1)
		{
			if (asset == 6) // 전체
			{
				query += " WHERE type in (1)";
			}
			else if (asset == 0) // 현금
			{
				query += " WHERE type in (1) and asset = " + asset;
			}
			else if (asset == 1) // 통장
			{
				query += " WHERE type in (1) and asset = " + asset;
			}
			else if (asset == 2) // 체크
			{
				query += " WHERE type = 1 and asset = " + asset;
			}
		}
		else if (type == 2)
		{
			if (asset == 6) // 전체
			{
				query += " WHERE type in (2, 7)";
			}
			else if (asset == 0) // 현금
			{
				query += " WHERE type in (2, 7) and (asset = " + asset + " or subasset = " + asset + ")";
			}
			else if (asset == 1) // 통장
			{
				query += " WHERE type in (2, 7) and (asset = " + asset + " or subasset = " + asset + ")";
			}
			else if (asset == 2) // 체크
			{
				query += " WHERE type in (2, 7) and (asset = " + asset + " or subasset = " + asset + ")";
			}
			else if (asset == 5) // 상품권
			{
				query += " WHERE type = 7 and asset = " + asset;
			}
		}
		else if (type == 3)
		{
			
		}
		
		if (!name.equals(""))
		{
			if (type == 0 || type == 2)
			{
				if (asset != 5 && asset != 6)
				{
					query += " and (name = '" + MilkUtil.getApostrophe(name) + 
								"' or subname = '" + MilkUtil.getApostrophe(name) + "')";
				}
			}
			else
			{
				query += " and name = '" + MilkUtil.getApostrophe(name) + "'";
			}
		}
		
		query += " and date >= " + firstDate + " and date <= " + lastDate + 
				 " ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type"); // 내역타입
		int kindIdx = c.getColumnIndex("kind"); // 내역종류
		int assetIdx = c.getColumnIndex("asset"); // 자산종류
		int subassetIdx = c.getColumnIndex("subasset"); // 자산종류
		int maincategoryIdx = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategoryIdx = c.getColumnIndex("subcategory"); // 서브카테고리
		int dateIdx = c.getColumnIndex("date"); // 날짜
		int ampmIdx = c.getColumnIndex("ampm"); // ampm 플레그
		int timehourIdx = c.getColumnIndex("timehour"); // 시간
		int timeminuteIdx = c.getColumnIndex("timeminute"); // 분
		int nameIdx = c.getColumnIndex("name"); // 자산이름
		int subnameIdx = c.getColumnIndex("subname"); // 서브자산이름
		int moneyIdx = c.getColumnIndex("money"); // 금액
		int pointIdx = c.getColumnIndex("point"); // 카드포인트
		int susumoneyIdx = c.getColumnIndex("susumoney"); // 수수료
		int personIdx = c.getColumnIndex("person"); // 부채사람
		int photopathIdx = c.getColumnIndex("photopath"); // 사진연결주소
		int memoIdx = c.getColumnIndex("memo"); // 내용
		int linkidIdx = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyidIdx = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setKind(c.getInt(kindIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubasset(c.getInt(subassetIdx) + "");
			data.setMaincategory(c.getString(maincategoryIdx));
			data.setSubcategory(c.getString(subcategoryIdx));
			data.setDate(c.getInt(dateIdx) + "");
			data.setAmpm(c.getInt(ampmIdx) + "");
			data.setTimehour(c.getInt(timehourIdx) + "");
			data.setTimeminute(c.getInt(timeminuteIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setSubname(c.getString(subnameIdx));
			data.setMoney(c.getString(moneyIdx));
			data.setPoint(c.getString(pointIdx));
			data.setSusumoney(c.getString(susumoneyIdx));
			data.setPerson(c.getString(personIdx));
			data.setPhotopath(c.getString(photopathIdx));
			data.setMemo(c.getString(memoIdx));
			data.setLinkId(c.getInt(linkidIdx) + "");
			data.setSubmoney(c.getString(submoneyidIdx));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * kind 1 = 빌림, 2 = 빌려줌, 3 = 갚음, 4 = 받음
	 * @param kind
	 * @param firstDate
	 * @param lastDate
	 * @return
	 */
	public ArrayList<InOutData> getInOutMainFanList(int kind, String firstDate, String lastDate)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		String query = "SELECT * FROM " + TableName.IN_OUT + " WHERE type in (3) and kind = " + kind;
		
		query += " and date >= " + firstDate + " and date <= " + lastDate + 
				 " ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type"); // 내역타입
		int kindIdx = c.getColumnIndex("kind"); // 내역종류
		int assetIdx = c.getColumnIndex("asset"); // 자산종류
		int subassetIdx = c.getColumnIndex("subasset"); // 자산종류
		int maincategoryIdx = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategoryIdx = c.getColumnIndex("subcategory"); // 서브카테고리
		int dateIdx = c.getColumnIndex("date"); // 날짜
		int ampmIdx = c.getColumnIndex("ampm"); // ampm 플레그
		int timehourIdx = c.getColumnIndex("timehour"); // 시간
		int timeminuteIdx = c.getColumnIndex("timeminute"); // 분
		int nameIdx = c.getColumnIndex("name"); // 자산이름
		int subnameIdx = c.getColumnIndex("subname"); // 서브자산이름
		int moneyIdx = c.getColumnIndex("money"); // 금액
		int pointIdx = c.getColumnIndex("point"); // 카드포인트
		int susumoneyIdx = c.getColumnIndex("susumoney"); // 수수료
		int personIdx = c.getColumnIndex("person"); // 부채사람
		int photopathIdx = c.getColumnIndex("photopath"); // 사진연결주소
		int memoIdx = c.getColumnIndex("memo"); // 내용
		int linkidIdx = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyidIdx = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setKind(c.getInt(kindIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubasset(c.getInt(subassetIdx) + "");
			data.setMaincategory(c.getString(maincategoryIdx));
			data.setSubcategory(c.getString(subcategoryIdx));
			data.setDate(c.getInt(dateIdx) + "");
			data.setAmpm(c.getInt(ampmIdx) + "");
			data.setTimehour(c.getInt(timehourIdx) + "");
			data.setTimeminute(c.getInt(timeminuteIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setSubname(c.getString(subnameIdx));
			data.setMoney(c.getString(moneyIdx));
			data.setPoint(c.getString(pointIdx));
			data.setSusumoney(c.getString(susumoneyIdx));
			data.setPerson(c.getString(personIdx));
			data.setPhotopath(c.getString(photopathIdx));
			data.setMemo(c.getString(memoIdx));
			data.setLinkId(c.getInt(linkidIdx) + "");
			data.setSubmoney(c.getString(submoneyidIdx));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * type 0 = 지출, 1 = 수입, 2 = 이동, 4 = 잔액
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체 
	 * @return
	 */
	public MainSettingData getMainSettingId(String id)
	{
		MainSettingData data = new MainSettingData();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.MAIN_SETTING + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type"); // 타입
		int assetIdx = c.getColumnIndex("asset"); // 자산종류
		int nameIdx = c.getColumnIndex("name"); // 자산이름
		int kindIdx = c.getColumnIndex("kind"); // 종류
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setKind(c.getInt(kindIdx) + "");
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * type 0 = 지출, 1 = 수입, 2 = 이동, 4 = 잔액
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체 
	 * @return
	 */
	public ArrayList<MainSettingData> getMainSettingAssetToName(String asset, String name)
	{
		ArrayList<MainSettingData> list = new ArrayList<MainSettingData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.MAIN_SETTING + " WHERE asset = " + asset + 
				" and name = '" + MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type");
		int assetIdx = c.getColumnIndex("asset");
		int nameIdx = c.getColumnIndex("name");
		int kindIdx = c.getColumnIndex("kind");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			MainSettingData data = new MainSettingData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setKind(c.getInt(kindIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * type 0 = 지출, 1 = 수입, 2 = 이동, 4 = 잔액
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체 
	 * @return
	 */
	public ArrayList<WidgetSettingData> getWidgetSettingList()
	{
		ArrayList<WidgetSettingData> list = new ArrayList<WidgetSettingData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.WIDGET_SETTING + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type"); // 타입
		int assetIdx = c.getColumnIndex("asset"); // 종류
		int nameIdx = c.getColumnIndex("name"); // 예산금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			WidgetSettingData data = new WidgetSettingData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setName(c.getString(nameIdx));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * type 0 = 지출, 1 = 수입, 2 = 이동, 4 = 잔액
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체 
	 * @return
	 */
	public WidgetSettingData getWidgetSettingId(String id)
	{
		WidgetSettingData data = new WidgetSettingData();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.WIDGET_SETTING + " WHERE id = " + id + ";";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type"); // 타입
		int assetIdx = c.getColumnIndex("asset"); // 종류
		int nameIdx = c.getColumnIndex("name"); // 예산금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setName(c.getString(nameIdx));
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return data;
	}
	
	/**
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체 
	 * @param asset
	 * @param name
	 * @return
	 */
	public ArrayList<InOutData> getInOutRollBackList(int asset, String name)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		if (name == null || name.equals("") || asset < 0 || asset > 5)
		{
			return list;
		}
		
		String query = "SELECT * FROM " + TableName.IN_OUT;
		
		
		if (asset == 0) // 현금
		{
			query += " WHERE asset = " + asset;
		}
		else if (asset == 1) // 통장
		{
			query += " WHERE asset = " + asset;
		}
		else if (asset == 2) // 체크
		{
			query += " WHERE asset = " + asset;
		}
		else if (asset == 3) // 신용
		{
			query += " WHERE asset = " + asset;
		}
		else if (asset == 4) // 핸드폰
		{
			query += " WHERE asset = " + asset;
		}
		else if (asset == 5) // 상품권
		{
			query += " WHERE asset = " + asset;
		}
		
		query += " and name = '" + MilkUtil.getApostrophe(name) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type"); // 내역타입
		int kindIdx = c.getColumnIndex("kind"); // 내역종류
		int assetIdx = c.getColumnIndex("asset"); // 자산종류
		int subassetIdx = c.getColumnIndex("subasset"); // 자산종류
		int maincategoryIdx = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategoryIdx = c.getColumnIndex("subcategory"); // 서브카테고리
		int dateIdx = c.getColumnIndex("date"); // 날짜
		int ampmIdx = c.getColumnIndex("ampm"); // ampm 플레그
		int timehourIdx = c.getColumnIndex("timehour"); // 시간
		int timeminuteIdx = c.getColumnIndex("timeminute"); // 분
		int nameIdx = c.getColumnIndex("name"); // 자산이름
		int subnameIdx = c.getColumnIndex("subname"); // 서브자산이름
		int moneyIdx = c.getColumnIndex("money"); // 금액
		int pointIdx = c.getColumnIndex("point"); // 카드포인트
		int susumoneyIdx = c.getColumnIndex("susumoney"); // 수수료
		int personIdx = c.getColumnIndex("person"); // 부채사람
		int photopathIdx = c.getColumnIndex("photopath"); // 사진연결주소
		int memoIdx = c.getColumnIndex("memo"); // 내용
		int linkidIdx = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyidIdx = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setKind(c.getInt(kindIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubasset(c.getInt(subassetIdx) + "");
			data.setMaincategory(c.getString(maincategoryIdx));
			data.setSubcategory(c.getString(subcategoryIdx));
			data.setDate(c.getInt(dateIdx) + "");
			data.setAmpm(c.getInt(ampmIdx) + "");
			data.setTimehour(c.getInt(timehourIdx) + "");
			data.setTimeminute(c.getInt(timeminuteIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setSubname(c.getString(subnameIdx));
			data.setMoney(c.getString(moneyIdx));
			data.setPoint(c.getString(pointIdx));
			data.setSusumoney(c.getString(susumoneyIdx));
			data.setPerson(c.getString(personIdx));
			data.setPhotopath(c.getString(photopathIdx));
			data.setMemo(c.getString(memoIdx));
			data.setLinkId(c.getInt(linkidIdx) + "");
			data.setSubmoney(c.getString(submoneyidIdx));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권, 6 = 전체 
	 * @param asset
	 * @param name
	 * @return
	 */
	public ArrayList<InOutData> getInOutRollBackSubList(int subasset, String subname)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		if (subname == null || subname.equals("") || subasset < 0 || subasset > 5)
		{
			return list;
		}
		
		String query = "SELECT * FROM " + TableName.IN_OUT;
		
		
		if (subasset == 0) // 현금
		{
			query += " WHERE subasset = " + subasset;
		}
		else if (subasset == 1) // 통장
		{
			query += " WHERE subasset = " + subasset;
		}
		else if (subasset == 2) // 체크
		{
			query += " WHERE subasset = " + subasset;
		}
		else if (subasset == 3) // 신용
		{
			query += " WHERE subasset = " + subasset;
		}
		else if (subasset == 4) // 핸드폰
		{
			query += " WHERE subasset = " + subasset;
		}
		else if (subasset == 5) // 상품권
		{
			query += " WHERE subasset = " + subasset;
		}
		
		query += " and subname = '" + MilkUtil.getApostrophe(subname) + "';";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int typeIdx = c.getColumnIndex("type"); // 내역타입
		int kindIdx = c.getColumnIndex("kind"); // 내역종류
		int assetIdx = c.getColumnIndex("asset"); // 자산종류
		int subassetIdx = c.getColumnIndex("subasset"); // 자산종류
		int maincategoryIdx = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategoryIdx = c.getColumnIndex("subcategory"); // 서브카테고리
		int dateIdx = c.getColumnIndex("date"); // 날짜
		int ampmIdx = c.getColumnIndex("ampm"); // ampm 플레그
		int timehourIdx = c.getColumnIndex("timehour"); // 시간
		int timeminuteIdx = c.getColumnIndex("timeminute"); // 분
		int nameIdx = c.getColumnIndex("name"); // 자산이름
		int subnameIdx = c.getColumnIndex("subname"); // 서브자산이름
		int moneyIdx = c.getColumnIndex("money"); // 금액
		int pointIdx = c.getColumnIndex("point"); // 카드포인트
		int susumoneyIdx = c.getColumnIndex("susumoney"); // 수수료
		int personIdx = c.getColumnIndex("person"); // 부채사람
		int photopathIdx = c.getColumnIndex("photopath"); // 사진연결주소
		int memoIdx = c.getColumnIndex("memo"); // 내용
		int linkidIdx = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyidIdx = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setKind(c.getInt(kindIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubasset(c.getInt(subassetIdx) + "");
			data.setMaincategory(c.getString(maincategoryIdx));
			data.setSubcategory(c.getString(subcategoryIdx));
			data.setDate(c.getInt(dateIdx) + "");
			data.setAmpm(c.getInt(ampmIdx) + "");
			data.setTimehour(c.getInt(timehourIdx) + "");
			data.setTimeminute(c.getInt(timeminuteIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setSubname(c.getString(subnameIdx));
			data.setMoney(c.getString(moneyIdx));
			data.setPoint(c.getString(pointIdx));
			data.setSusumoney(c.getString(susumoneyIdx));
			data.setPerson(c.getString(personIdx));
			data.setPhotopath(c.getString(photopathIdx));
			data.setMemo(c.getString(memoIdx));
			data.setLinkId(c.getInt(linkidIdx) + "");
			data.setSubmoney(c.getString(submoneyidIdx));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권
	 * @param asset
	 * @param name
	 * @return
	 */
	public ArrayList<SmsData> getSmsAssetList(String asset, String name)
	{
		ArrayList<SmsData> list = new ArrayList<SmsData>();
		
		String query = "SELECT * FROM " + TableName.SMS + 
						" WHERE asset = " + asset + " and "
								+ "cardname = '" + MilkUtil.getApostrophe(name) + "' or "
										+ "subcardname = '" + MilkUtil.getApostrophe(name) + "';";
		
		Cursor c= db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int nameIdx = c.getColumnIndex("name");
		int parserIdx = c.getColumnIndex("parser");
		int addressIdx = c.getColumnIndex("address");
		int memoIdx = c.getColumnIndex("memo");
		int cardnameIdx = c.getColumnIndex("cardname");
		int assetIdx = c.getColumnIndex("asset");
		int subcardnameIdx = c.getColumnIndex("subcardname");
		int subassetIdx = c.getColumnIndex("subasset");
		int typeIdx = c.getColumnIndex("type");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			SmsData data = new SmsData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setParser(c.getInt(parserIdx) + "");
			data.setAddress(c.getString(addressIdx));
			data.setMemo(c.getString(memoIdx));
			data.setCardname(c.getString(cardnameIdx));
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubCardname(c.getString(subcardnameIdx));
			data.setSubAsset(c.getInt(subassetIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권
	 * @param flag
	 * @return
	 */
	public ArrayList<AutoMainData> getAutoAssetList(String asset, String name)
	{
		ArrayList<AutoMainData> list = new ArrayList<AutoMainData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.AUTO_MAIN + " WHERE asset = " + asset + 
				" and name = '" + MilkUtil.getApostrophe(name) + 
				"' ORDER BY autodate DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int monthweekdayIdx = c.getColumnIndex("monthweekday"); // 주간 월간 플래그
		int typeIdx = c.getColumnIndex("type"); // 내역타입
		int kindIdx = c.getColumnIndex("kind"); // 내역종류
		int assetIdx = c.getColumnIndex("asset"); // 자산종류
		int subassetIdx = c.getColumnIndex("subasset"); // 자산종류
		int maincategoryIdx = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategoryIdx = c.getColumnIndex("subcategory"); // 서브카테고리
		int ampmIdx = c.getColumnIndex("ampm"); // ampm 플레그
		int timehourIdx = c.getColumnIndex("timehour"); // 시간
		int timeminuteIdx = c.getColumnIndex("timeminute"); // 분
		int nameIdx = c.getColumnIndex("name"); // 자산이름
		int subnameIdx = c.getColumnIndex("subname"); // 서브자산이름
		int moneyIdx = c.getColumnIndex("money"); // 금액
		int susumoneyIdx = c.getColumnIndex("susumoney"); // 수수료
		int memoIdx = c.getColumnIndex("memo"); // 내용
		int submoneyIdx = c.getColumnIndex("submoney"); // 서브 금액
		int autodateIdx = c.getColumnIndex("autodate"); // 고정날짜
		int signdateIdx = c.getColumnIndex("signdate"); // 적용날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			AutoMainData data = new AutoMainData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setMonthweekday(c.getInt(monthweekdayIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setKind(c.getInt(kindIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubasset(c.getInt(subassetIdx) + "");
			data.setMaincategory(c.getString(maincategoryIdx));
			data.setSubcategory(c.getString(subcategoryIdx));
			data.setAmpm(c.getInt(ampmIdx) + "");
			data.setTimehour(c.getInt(timehourIdx) + "");
			data.setTimeminute(c.getInt(timeminuteIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setSubname(c.getString(subnameIdx));
			data.setMoney(c.getString(moneyIdx));
			data.setSusumoney(c.getString(susumoneyIdx));
			data.setMemo(c.getString(memoIdx));
			data.setSubmoney(c.getString(submoneyIdx));
			data.setAutodate(c.getInt(autodateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * asset 0 = 현금, 1 = 통장, 2 = 체크, 3 = 신용, 4 = 핸드폰, 5 = 상품권
	 * @param asset
	 * @param name
	 * @return
	 */
	public ArrayList<HalbuData> getHalbuAssetList(String asset, String name)
	{
		ArrayList<HalbuData> list = new ArrayList<HalbuData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.HALBU + 
				" WHERE asset = " + asset + " and name = '" + MilkUtil.getApostrophe(name) + "'" +
				" ORDER BY autodate DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int halbucountIdx = c.getColumnIndex("halbucount"); // 개월수
		int resultcountIdx = c.getColumnIndex("resultcount"); // 남은 개월수
		int typeIdx = c.getColumnIndex("type"); // 내역타입
		int kindIdx = c.getColumnIndex("kind"); // 내역종류
		int assetIdx = c.getColumnIndex("asset"); // 자산종류
		int subassetIdx = c.getColumnIndex("subasset"); // 자산종류
		int maincategoryIdx = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategoryIdx = c.getColumnIndex("subcategory"); // 서브카테고리
		int ampmIdx = c.getColumnIndex("ampm"); // ampm 플레그
		int timehourIdx = c.getColumnIndex("timehour"); // 시간
		int timeminuteIdx = c.getColumnIndex("timeminute"); // 분
		int nameIdx = c.getColumnIndex("name"); // 자산이름
		int subnameIdx = c.getColumnIndex("subname"); // 서브자산이름
		int moneyIdx = c.getColumnIndex("money"); // 금액
		int susumoneyIdx = c.getColumnIndex("susumoney"); // 수수료
		int memoIdx = c.getColumnIndex("memo"); // 내용
		int submoneyIdx = c.getColumnIndex("submoney"); // 서브 금액
		int autodateIdx = c.getColumnIndex("autodate"); // 고정날짜
		int signdateIdx = c.getColumnIndex("signdate"); // 적용날짜
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			HalbuData data = new HalbuData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setHalbucount(c.getInt(halbucountIdx) + "");
			data.setResultcount(c.getInt(resultcountIdx) + "");
			data.setType(c.getInt(typeIdx) + "");
			data.setKind(c.getInt(kindIdx) + "");
			data.setAsset(c.getInt(assetIdx) + "");
			data.setSubasset(c.getInt(subassetIdx) + "");
			data.setMaincategory(c.getString(maincategoryIdx));
			data.setSubcategory(c.getString(subcategoryIdx));
			data.setAmpm(c.getInt(ampmIdx) + "");
			data.setTimehour(c.getInt(timehourIdx) + "");
			data.setTimeminute(c.getInt(timeminuteIdx) + "");
			data.setName(c.getString(nameIdx));
			data.setSubname(c.getString(subnameIdx));
			data.setMoney(c.getString(moneyIdx));
			data.setSusumoney(c.getString(susumoneyIdx));
			data.setMemo(c.getString(memoIdx));
			data.setSubmoney(c.getString(submoneyIdx));
			data.setAutodate(c.getInt(autodateIdx) + "");
			data.setSigndate(c.getInt(signdateIdx) + "");
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * @return
	 */
	public ArrayList<InOutData> getInOutMainCategoryList(String mainCategory)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.IN_OUT + " WHERE" + 
				" maincategory = '" + MilkUtil.getApostrophe(mainCategory) + "'" + 
				" ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int date = c.getColumnIndex("date"); // 날짜
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int point = c.getColumnIndex("point"); // 카드포인트
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int person = c.getColumnIndex("person"); // 부채사람
		int photopath = c.getColumnIndex("photopath"); // 사진연결주소
		int memo = c.getColumnIndex("memo"); // 내용
		int linkid = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(id) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setDate(c.getInt(date) + "");
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setPoint(c.getString(point));
			data.setSusumoney(c.getString(susumoney));
			data.setPerson(c.getString(person));
			data.setPhotopath(c.getString(photopath));
			data.setMemo(c.getString(memo));
			data.setLinkId(c.getInt(linkid) + "");
			data.setSubmoney(c.getString(submoneyid));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	/**
	 * @return
	 */
	public ArrayList<InOutData> getInOutSubCategoryList(String mainCategory, String subCategory)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.IN_OUT + " WHERE" + 
				" maincategory = '" + MilkUtil.getApostrophe(mainCategory) + 
				"' and subcategory = '" + MilkUtil.getApostrophe(subCategory) + "'" +
				" ORDER BY date DESC, ampm DESC, timehour DESC, timeminute DESC;";
		
		Cursor c = db.select(context, query);
		
		int id = c.getColumnIndex("id");
		int type = c.getColumnIndex("type"); // 내역타입
		int kind = c.getColumnIndex("kind"); // 내역종류
		int asset = c.getColumnIndex("asset"); // 자산종류
		int subasset = c.getColumnIndex("subasset"); // 자산종류
		int maincategory = c.getColumnIndex("maincategory"); // 메인카테고리
		int subcategory = c.getColumnIndex("subcategory"); // 서브카테고리
		int date = c.getColumnIndex("date"); // 날짜
		int ampm = c.getColumnIndex("ampm"); // ampm 플레그
		int timehour = c.getColumnIndex("timehour"); // 시간
		int timeminute = c.getColumnIndex("timeminute"); // 분
		int name = c.getColumnIndex("name"); // 자산이름
		int subname = c.getColumnIndex("subname"); // 서브자산이름
		int money = c.getColumnIndex("money"); // 금액
		int point = c.getColumnIndex("point"); // 카드포인트
		int susumoney = c.getColumnIndex("susumoney"); // 수수료
		int person = c.getColumnIndex("person"); // 부채사람
		int photopath = c.getColumnIndex("photopath"); // 사진연결주소
		int memo = c.getColumnIndex("memo"); // 내용
		int linkid = c.getColumnIndex("linkid"); // 연결 아이디
		int submoneyid = c.getColumnIndex("submoney"); // 서브 금액
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			InOutData data = new InOutData();
			
			data.setId(c.getInt(id) + "");
			data.setType(c.getInt(type) + "");
			data.setKind(c.getInt(kind) + "");
			data.setAsset(c.getInt(asset) + "");
			data.setSubasset(c.getInt(subasset) + "");
			data.setMaincategory(c.getString(maincategory));
			data.setSubcategory(c.getString(subcategory));
			data.setDate(c.getInt(date) + "");
			data.setAmpm(c.getInt(ampm) + "");
			data.setTimehour(c.getInt(timehour) + "");
			data.setTimeminute(c.getInt(timeminute) + "");
			data.setName(c.getString(name));
			data.setSubname(c.getString(subname));
			data.setMoney(c.getString(money));
			data.setPoint(c.getString(point));
			data.setSusumoney(c.getString(susumoney));
			data.setPerson(c.getString(person));
			data.setPhotopath(c.getString(photopath));
			data.setMemo(c.getString(memo));
			data.setLinkId(c.getInt(linkid) + "");
			data.setSubmoney(c.getString(submoneyid));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		if(c != null) c.close();
		
		return list;
	}
	
	public ArrayList<String> getHouseTitleList()
	{
		ArrayList<String> list = new ArrayList<String>();
		
		String query = "";
		
		query = "SELECT title FROM " + TableName.APT_DATA + " GROUP BY title ORDER BY title ASC"; 
		
		Cursor c = db.select(context, query);
		
		int titleIdx = c.getColumnIndex("title");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			String data = c.getString(titleIdx);
			
			list.add(data);
			
			c.moveToNext();
		}
		
		return list;
	}
	
	public ArrayList<AptData> getHouseList(String title)
	{
		ArrayList<AptData> list = new ArrayList<AptData>();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.APT_DATA + " WHERE title = '" + title + "'" + 
				" ORDER BY name ASC";
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int titleIdx = c.getColumnIndex("title");
		int nameIdx = c.getColumnIndex("name");
		int meibIdx = c.getColumnIndex("meib");
		int junseIdx = c.getColumnIndex("junse");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			AptData data = new AptData();
			
			data.setId(c.getInt(idIdx) + "");
			data.setTitle(c.getString(titleIdx));
			data.setName(c.getString(nameIdx));
			data.setMeib(c.getString(meibIdx));
			data.setJunse(c.getString(junseIdx));
			
			list.add(data);
			
			c.moveToNext();
		}
		
		return list;
	}
	
	public AptData getHouseId(String id)
	{
		AptData data = new AptData();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.APT_DATA + " WHERE id = " + id + ""; 
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int titleIdx = c.getColumnIndex("title");
		int nameIdx = c.getColumnIndex("name");
		int meibIdx = c.getColumnIndex("meib");
		int junseIdx = c.getColumnIndex("junse");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setTitle(c.getString(titleIdx));
			data.setName(c.getString(nameIdx));
			data.setMeib(c.getString(meibIdx));
			data.setJunse(c.getString(junseIdx));
			
			c.moveToNext();
		}
		
		return data;
	}
	
	public AptData getHouseTitleName(String title, String name)
	{
		AptData data = new AptData();
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.APT_DATA + " WHERE title = '" + title + "'" +
				" and name = '" + name + "';"; 
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int titleIdx = c.getColumnIndex("title");
		int nameIdx = c.getColumnIndex("name");
		int meibIdx = c.getColumnIndex("meib");
		int junseIdx = c.getColumnIndex("junse");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data.setId(c.getInt(idIdx) + "");
			data.setTitle(c.getString(titleIdx));
			data.setName(c.getString(nameIdx));
			data.setMeib(c.getString(meibIdx));
			data.setJunse(c.getString(junseIdx));
			
			c.moveToNext();
		}
		
		return data;
	}
	
	public MyHouseData getMyHouse()
	{
		MyHouseData data = null;
		
		String query = "";
		
		query = "SELECT * FROM " + TableName.MY_HOUSE;
		
		Cursor c = db.select(context, query);
		
		int idIdx = c.getColumnIndex("id");
		int titleIdx = c.getColumnIndex("title");
		int nameIdx = c.getColumnIndex("name");
		int pungIdx = c.getColumnIndex("pung");
		int dateIdx = c.getColumnIndex("date");
		int flagIdx = c.getColumnIndex("flag");
		
		c.moveToFirst();
		
		for (int i = 0; i < c.getCount(); i++)
		{
			data = new MyHouseData();
			data.setId(c.getInt(idIdx) + "");
			data.setTitle(c.getString(titleIdx));
			data.setName(c.getString(nameIdx));
			data.setPung(c.getString(pungIdx));
			data.setDate(c.getInt(dateIdx) + "");
			data.setFlag(c.getInt(flagIdx) + "");
			
			c.moveToNext();
		}
		
		return data;
	}
	
	/**
	 * 해당 테이블에 데이터가 있는지 확인한다.
	 * @param tableName
	 * @return
	 */
	public int getDBTableDataCount(String tableName)
	{
		String query = "";
		
		query = "SELECT * FROM " + tableName;
		
		Cursor c = db.select(context, query);
		
		int dbCount = 0;
		
		dbCount = c.getCount();
		
		c.close();
		
		return dbCount;
	}
}
